package com.gzc.dao.imp;

import org.springframework.stereotype.Repository;

import com.gzc.dao.IMessageDao;
import com.gzc.model.Message;
import com.gzc.model.UserReceivedMessage;

@Repository("messageDao")
public class MessageDao extends BaseDao<Message> implements IMessageDao {
	/**
	 * 检查某个用户是否已经读过该msg
	 * @param userId 要检查的用户id
	 * @param msgId 私人信件id
	 * @return
	 */
	public boolean checkIsRead(int userId, int msgId){
		final String hql = "select um.isRead from UserReceivedMessage as um where um.user.id=? and um.message.id=?";
		Integer isRead = (Integer)this.queryByHql(hql, new Object[]{ userId, msgId });
		if(null == isRead || 0 == isRead){
			return false;
		}		
		return true;
	}
	/**
	 * 根据用户id和私人信件id加载UserReceivedMessage对象
	 * @param userId 用户id
	 * @param msgId 私人信件id
	 * @return 用户接受到的消息
	 */
	@Override
	public UserReceivedMessage loadUserMessage(int userId, int msgId) {
		final String hql = "select um from UserReceivedMessage as um where um.user.id=? and um.message.id=?";
		return (UserReceivedMessage)this.queryByHql(hql, new Object[]{ userId, msgId });
	}

}
